
package tests.laurens.cajo_remote_stub;

import gnu.cajo.invoke.Remote;
import gnu.cajo.utils.extra.TransparentItemProxy;

import combat.dudes.protocol.data.RemoteList;
import combat.dudes.protocol.interfaces.SimulationInt;
import combat.dudes.protocol.interfaces.TeamInt;
import combat.dudes.protocol.interfaces.UserInt;


/**
 * @author Laurens Andriessen
 * @version 1.0
 */
public class TestClientCombat {
	public static void main(String[] args) throws Exception {

		Object simulation = Remote.getItem("//localhost:28000/Simulation");
		SimulationInt sim = (SimulationInt) TransparentItemProxy.getItem(simulation, new Class[] { SimulationInt.class });


		Object meRemote = Remote.invoke(simulation, "userLogOn_Remote", new Object[] { "Niels123456" });


		UserInt me = (UserInt) TransparentItemProxy.getItem(meRemote, new Class[] { UserInt.class });
		me.setUserName("Niels 1");

		RemoteList teamsRemote = (RemoteList) Remote.invoke(simulation, "getTeams_Remote", null);
		System.out.println("Aantal teams: " + teamsRemote.size());

		// TeamInt teamz0rz = (TeamInt) TransparentItemProxy.getItem(teamsRemote.get(2), new Class[] { TeamInt.class });
		Object teamz0rz = TransparentItemProxy.getItem(teamsRemote.get(1), new Class[] { TeamInt.class });
		sim.switchTeam_Remote(me, (TeamInt) teamz0rz);


		// for(Object veh : sim.getTeams().get(0).getProjectiles_Remote()) {
		// sim.findPhysicalObject(TransparentItemProxy.getItem(veh, new Class[] { ProjectileInt.class }));
		// }


		System.exit(0);


		// Object teamz0rz = Remote.invoke(teamsRemote, "get", new Object[] { 2 });
		TeamInt teamA = (TeamInt) TransparentItemProxy.getItem(teamz0rz, new Class[] { TeamInt.class });
		TeamInt teamB = (TeamInt) TransparentItemProxy.getItem(teamA, new Class[] { TeamInt.class });

		System.out.println("A: " + teamA.hashCode());
		System.out.println("B: " + teamB.hashCode());

		System.out.println(teamB.getClass().toString());
		// Object uh = Remote.toStub(teamz0rz);

		// Object wat = Remote.toStub(teamz0rz);
		// System.out.println(wat.getClass());
		// sim.switchTeam_Remote(me, teamz0rz);
		// Proxy wut = (Proxy) Remote.autobox(TeamInt.class).cast(teamz0rz);

		// System.out.println("___ " + Proxy.getProxyClass(teamz0rz.getClass().getClassLoader(), TeamInt.class.getInterfaces()));
		// sim.switchTeam_Remote(me, (TeamInt) teamz0rz);
		RemoteList t = sim.getTeams_Remote();
		TeamInt tt = (TeamInt) TransparentItemProxy.getItem(t.get(2), new Class[] { TeamInt.class });
		sim.switchTeam_Remote(me, tt);
		// Remote.invoke(simulation, "switchTeam_Remote", new Object[] { me, teamz0rz });

		for(Object teamRemote : teamsRemote) {
			System.out.println("");
			RemoteList users = (RemoteList) Remote.invoke(teamRemote, "getUsers_Remote", null);
			for(Object userRemote : users) {
				UserInt user = (UserInt) TransparentItemProxy.getItem(userRemote, new Class[] { UserInt.class });
				System.out.println("w00t " + user.getUserName());
			}
		}


		/* ArrayList<UserInt> users = (ArrayList<UserInt>) Remote.invoke(teams.get(0), "getUsers", null);
		 * System.out.println("Aantal users in team 0: " + users.size());
		 * 
		 * 
		 * UserInt user = (UserInt) Remote.invoke(users, "get", new Object[] { 0 });
		 * 
		 * String userName1 = (String) Remote.invoke(user, "getUserName", null);
		 * System.out.println(userName1);
		 * 
		 * Remote.invoke(user, "setUserName", new Object[] { "Piet" });
		 * 
		 * String userName2 = (String) Remote.invoke(user, "getUserName", null);
		 * System.out.println(userName2);
		 * 
		 * int i = 0;
		 * while(true) {
		 * Thread.sleep(1500);
		 * System.out.println(i++ + " " + Remote.invoke(user, "getUserName", null));
		 * }
		 * 
		 * // SimulationInt sim = (SimulationInt) TransparentItemProxy.getItem(DudesConfig.serverUrl + "Simulation", new Class[] { SimulationInt.class });
		 * // System.out.println("1-" + sim.getTeams().get(0).getUsers().get(0).getUserName());
		 * // sim.getTeams().get(0).getUsers().get(0).setUserName("TEST");
		 * // System.out.println("2-" + sim.getTeams().get(0).getUsers().get(0).getUserName()); */
	}
}
